import { queryBarData, queryDeviceList, queryDeviceTable, queryLineData, queryUser } from "../dao/pageDao1.js";

const path = new Map();
function getDateStr(date) {
  let timeString = date.getFullYear() + "-" +
    (date.getMonth() + 1) + "-" +
    date.getDate() + " " +
    date.getHours() + ":" +
    date.getMinutes() + ":" + date.getSeconds();
  return timeString
}
function getBarChatData(req, res) {
  console.log('调用了 getBarChatData ');
  // var deviceId = req.query.deviceId;
  // if (!deviceId || deviceId == null) {
  //   deviceId = '00001006000000000165'
  // }
  // console.log('get device id = ', deviceId);
  queryBarData(function (result) {
    res.writeHead(200);
    var xAxis = [];
    let maxMeteValue;
    let minMeteValue;
    let maxPueValue = 1.6;
    let minPueValue = 1;
    // let pueList = [];
    let allList = [];
    let itList = [];
    let ktList = [];
    let qtList = [];

    result.forEach(item => {
      xAxis.push(item.time);

      if (!maxMeteValue || maxMeteValue < item.all_dl) {
        maxMeteValue = item.all_dl;
      }
      if (!minMeteValue || minMeteValue > item.qt_dl) {
        minMeteValue = item.qt_dl;
      }
      if (!maxPueValue || maxPueValue < item.pue) {
        maxPueValue = item.pue;
      }
      // if (!minPueValue || minPueValue > item.pue) {
      //   minPueValue = item.pue;
      // }

      // pueList.push(item.pue);
      allList.push(item.all_dl);
      itList.push(item.it_dl);
      ktList.push(item.kt_dl);
      qtList.push(item.qt_dl);

    });

    var itemList = ['PUE', '总用电量', 'IT用电量', '空调用电量', '其他用电量'];
    var series = [
      // {
      //   name: 'PUE',
      //   type: 'line',
      //   yAxisIndex: 1,
      //   data: pueList
      // },
      {
        name: '总用电量',
        type: 'bar',
        yAxisIndex: 0,
        data: allList
      },
      {
        name: 'IT用电量',
        type: 'bar',
        yAxisIndex: 0,
        data: itList
      }, {
        name: '空调用电量',
        type: 'bar',
        yAxisIndex: 0,
        data: ktList
      },
      {
        name: '其他用电量',
        type: 'bar',
        yAxisIndex: 0,
        data: qtList
      }
    ]

    var result_json = {
      xAxis: xAxis,
      max: maxMeteValue,
      itemList: itemList,
      series: series,
      min: minMeteValue,
      maxPue: maxPueValue,
      minPue: minPueValue
    }
    // console.log('last result = ', result_json);
    res.write(JSON.stringify(result_json));
    res.end();
  });
}

function getDeviceList(req, res) {
  console.log('调用了 getDeviceList');
  queryDeviceList(function (result) {
    res.writeHead(200);
    res.write(JSON.stringify(result));
    res.end();
  });
}


function getDeviceMeteTable(req, res) {
  console.log('调用了 getDeviceList');
  queryDeviceTable(function (result) {
    res.writeHead(200);
    var dateList = ['机房名称', '设备名称', '信号名称', '信号值', '记录时间', '上次记录值', '事件', '来源系统']
    var titleList = ['precinct_name', 'device_name', 'mete_name', 'mete_value', 'record_time', 'last_mete_value', 'desc', 'dict_note'];
    var dataModel = {};
    var dataList = [];
    // 获取日期列表
    let dataStr = ''

    result.forEach(element => {
      var timeString = getDateStr(element.record_time);

      let tableStr = `<tr class=${element.desc}>`;
      titleList.forEach(item => {
        if ('record_time' != item) {
          tableStr += `<td>${element[item]}</td>`
        }
        else {
          tableStr += `<td>${timeString}</td>`;
        }
      });
      tableStr += '</tr>';
      dataStr += tableStr;

    });

    let resultJson = {};
    resultJson.dateList = dateList;
    resultJson.data = dataStr;
    res.write(JSON.stringify(resultJson));
    res.end();
  });
}


function getLineData(req, res) {
  console.log('调用了 getLineData ');

  queryLineData(function (result) {
    res.writeHead(200);
    let timeList = [];
    let data = [];

    result.forEach(element => {
      // meteNameList.push(element.mete_name);
      // let timeStr = getDateStr(element.record_time);
      let timeStr = element.time;

      if (timeList.includes(timeStr)) {

      } else {
        timeList.push(timeStr);
      }
      data.push(element.pue);
    });

    let resultJson = {
      meteNameList: timeList,
      data: data
    }
    res.write(JSON.stringify(resultJson));
    res.end();
  });

}


path.set('/getBarChatData', getBarChatData);
path.set('/getDeviceList', getDeviceList);
path.set('/getDeviceMeteTable', getDeviceMeteTable);
path.set('/getLineData', getLineData);


export { path, getDateStr };